// pages/market/address/add_user_address.js

import api from "../../../utils/api.js";
import market_cart from '../../../utils/market/market_cart'
import message from "../../../utils/message.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {

        address:{
            username:'',
            mobile:'',
            detail:'',
            address_name:'',
            area_name:'',
            area_id:''
        },
        market_address_list:[],
        is_select_address:false,
        type:'add',
        is_first:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var me = this;
        

        if(options.type && options.type=='edit'){
            wx.setNavigationBarTitle({
              title: '编辑收货地址',
            })
            this.getUserAddressById(options.id)
        }

        setTimeout(function () {
            me.setData({
                is_first:false
            })
            
            
        } , 500)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.getMarketAddressList();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    doAddAddress:function(){
        var me = this;
        var address = this.data.address;
        var type= this.data.type;

        if(type!='edit'){
            console.log(address)
            message.show_loading("操作执行中");
            market_cart.addMarketUserAddress( address , function(response){
                message.hide_loading();
                if(response.status==1){
                    message.show_error( response.message , function(){
                        wx.navigateBack({
                        delta: 1,
                        })
                    } )
                }
            })

        }else{
            message.show_loading("操作执行中");
            market_cart.updateUserAddress(address , function(response){
                message.hide_loading();
                if( response.status==1 ){
                    
                    message.show_error( response.message , function(){

                        wx.navigateBack({
                        delta: 1,
                        })
                    })
                }
            })
        }
        
    },
    inputBlur: function (e) {
        //console.log(JSON.stringify(e));
        var name = e.currentTarget.dataset.name;
        var value = e.detail.value;
        var address = this.data.address;
        address[name] = value;
        this.setData({
          address:address
        })
        //this.setData(JSON.parse(data));
    },
    getMarketAddressList:function(){
        //加载支持的配送区域
        var me = this;
        market_cart.getMarketAddressList({} , function(response){
            if(response.status==1){
                me.setData({
                    market_address_list:response.data
                })
            }
        })
    },
    showAddressList:function( e ){
        var action = parseInt(e.currentTarget.dataset.action);
        if(action==1){
            this.setData({
                is_select_address:true
            })
        }

        if(action==-1){
            this.setData({
                is_select_address:false
            })
        }

    },
    selectAddress:function( e ){
        var index = e.currentTarget.dataset.index;
        var exp_address = this.data.market_address_list[index];

        if(exp_address!=null){
            var address = this.data.address;
            address.area_id = exp_address.id;
            address.area_name = exp_address.area_name;

            this.setData({
                address:address,
                is_select_address:false
            })
        }

    },
    getUserAddressById:function(address_id){
        var me = this;
        var params = {}
        params.address_id = address_id;
        market_cart.getUserAddressById( params , function( response ){
            
            if(response.status==1){
                me.setData({
                    address:response.data,
                    type:'edit'
                })

            }else{
                message.show_error(response.message)
            }
        }); 
    }
})